<?php
/*
Plugin Name: KDA Slideshow
Plugin URI: http://kda-dev.com
Description: Show slide plugin
Author: Khoa Nguyen
Version: 1.0
Author URI: http://kda-dev.com
*/

$kda_slideshow = new kda_slideshow();
$kda_slideshow->add_menu();
class kda_slideshow
{
	function add_menu()
	{
		add_action('admin_menu', array('kda_slideshow', 'admin_add_menu'));
		add_action('wp_head', array('kda_slideshow', 'front_add_data'));
	}
	
	public static function admin_add_menu()
	{
		add_menu_page('KDA Slideshow', 'KDA Slideshow', 8, 'kda_slideshow-edit', array('kda_slideshow', 'edit_slide'));
		add_submenu_page('kda_slideshow-edit', 'KDA Slideshow Add', 8, 'kda_slideshow-add', array('kda_slideshow', 'edit_slide'));
	}
	

	public static function front_add_data()
	{
	$options = get_option('kda_slideshow');
	?>
<script type="text/javascript" src="<?php bloginfo('wpurl'); ?>/wp-content/plugins/jquery-slide-menu/js/jquery.min.js"></script> 
<script type="text/JavaScript">
$(document).ready(function() {
  // Declare variables
  var width = <?php echo stripslashes($options['data']['width']); ?>;
  var slides = $('#list-images div.slide');
  var numSlides = slides.length;
  var index = 0;
  var notClicked = 0;
  // Wrap the slides & set the wrap width - this will be used to animate the slider left/right
  slides.wrapAll('<div id="slide-wrap"></div>').css({'float' : 'left','width' : width});
  $('#slide-wrap').css({width: width * numSlides});
  
  // Click function - animate the slides based on index of links
  $('#list-links li a').click(function(){
	notClicked = 1;
	$('#list-links li').removeClass('hover');
	var i = $(this).index('#list-links li a');
	$('#slidesnumber').html(width*(-i));
	$(this).parent().addClass('hover');
	$('#slide-wrap').stop().animate({'marginLeft' : width*(-i)});
	$('#slidesnumber').html("Index"+i + "width:" + width*(-i));
  });
		setInterval(function() {
		var last_index;
			if(notClicked == 0){
			
				 
				 var max = $('#list-links li a').size()
				 if(index >= max-1){
				 	last_index = index;
					index = 0;
				 }
				 else{
				 	last_index = index;
					index++;
				 }
				 $('#list-links li').eq(last_index).removeClass('hover');
				 $('#list-links li').eq(index).addClass('hover');
				 $('#slide-wrap').stop().animate({'marginLeft' : width*(-index)});

			}
			
		 
		}, 5000);
		
});
</script>

<style type="text/css">
/* Tutorial CSS */
#menu-slider {
background: url(images/bg_menu_slider.png) no-repeat 0 0; 
padding: 15px; 
margin-bottom: 20px;
}

/* Required */
#list-images, #list-images li {
height: 292px; 
width: <?php echo stripslashes($options['data']['width']); ?>px; 
height: <?php echo stripslashes($options['data']['height']); ?>px; 
display: block;
}
#list-images {
float: right; 
overflow: hidden; /* Required to hide the inactive slides */
border: 1px solid #ccc;
}
#list-images li {
position: relative;
}

#list-images li img {
background: #fff; 
position: absolute; 
top: 0; 
left: 0;
}

/* Image captions */
#list-images li span {
background: url(images/grid1.png) repeat 0 0; 
position: absolute; 
bottom: 0; 
left: 0; 
width: 362px; 
display: block; 
padding: 14px 20px; 
font: bold 20px Arial, sans-serif; 
color: #fff; 
height: 20px; 
line-height: 20px;
}

/* Menu text links */
#list-links {
width: 220px; 
float: left;
}
#list-links li {
padding: 0 15px 0 0;
}
#list-links li a {
font: normal 12px Arial, sans-serif; 
color: #222; 
padding: 8px 5px 8px 8px; 
border-bottom: 1px solid #ccc; 
font-weight: bold; 
font-size: 13px; 
display: block;
}
#list-links li.hover {
background: url(images/tab_current.png) no-repeat 100% center;
}
#list-links li.hover a, #list-links li.hover a:hover {
color: #fff; 
background: none; 
border-bottom: none; 
padding-bottom: 9px;
}
#list-links li.last a {
border-bottom: none;
}
</style>
<?php
	}

	public static function show_slideShow()
	{
		$options = get_option('kda_slideshow');
	?>
	<div class="wrap">
		<div id="menu-slider">
			<div id="list-images">
			<?php
		if($options['data']['content'] != "")
		{
			foreach($options['data']['content'] as $slideContent){
				if($slideContent != ""){
					echo '<div class="slide">'.$slideContent.'</div>';
				}
			}
		}
	?>
			</div>
		</div>
	</div>	
	<?php
	}
	public static function edit_slide()
	{
		kda_slideshow::data_save();
		$options = get_option('kda_slideshow');	
		?>
		<div class="wrap">
			<form method="post" name="slide_menu_form">
				<h2>Slide Menu</h2>
				
				<h3>Enter html to show in slides, leave blank if you don't want to show that slide.</h3>
				<?php
					the_editor($options['data']['content']['slide'.$i], "slide".$i, "", true);
				?>
				
				<p>Width:<input id="slideWidth" name="slideWidth" size="10" value="<?php echo $options['data']['width']; ?>"></p>
				<p>Height:<input id="slideHeight" name="slideHeight" size="10" value="<?php echo $options['data']['height']; ?>"></p>
				<input type="submit" name="submitter" value="<?php esc_attr_e('Save Changes') ?>" class="button-primary" />
				
				<h4>Current Slide Menu</h4>
			</form>
		</div>
		<?php
	}
		function data_save()
	{
		if(isset($_POST['submitter']))
		{
			$option_name = 'kda_slideshow';
			for($i=1; $i <= 7; $i++){
				$options['data']['content']['slide'.$i] = $_POST['slide'.$i];
			}
			$options['data']['width'] = $_POST['slideWidth'];
			$options['data']['height'] = $_POST['slideHeight'];
			
			if ( get_option($option_name) )
				update_option($option_name, $options);
			else
				add_option( $option_name, $options );
		}
	}
	
}

//Post editor
add_action('admin_init', 'editor_admin_init');
add_action('admin_head', 'editor_admin_head');

function editor_admin_init() {
  wp_enqueue_script('word-count');
  wp_enqueue_script('post');
  wp_enqueue_script('editor');
  wp_enqueue_script('media-upload');
  wp_enqueue_style('thickbox');
  wp_enqueue_script('thickbox');
}

function editor_admin_head() {
  wp_tiny_mce();
}

?>
